import Vue from "vue";
import Vuex from "vuex";
import { reqProvince, reqLogin, reqNews, reqVip } from "@/api";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    Province: [],
    userinfo: {
      // userinfo: { username: "" },
    },
    News: [],
  },
  getters: {
    getName(state) {
      return state.userinfo.userinfo ? state.userinfo.userinfo.username : "";
    },
    getVip(state) {
      return state.userinfo.userinfo ? state.userinfo.userinfo.is_vip : "";
    },
  },
  mutations: {
    Province(state, payload) {
      state.Province = payload;
    },
    Userinfo(state, payload) {
      state.userinfo = payload;
    },
    News(state, payload) {
      state.News = payload;
    },
  },
  actions: {
    async getProvince({ commit }) {
      let { arr: res } = await reqProvince();
      console.log(res);
      commit("Province", res);
    },
    async getUser({ commit }, data) {
      let res = await reqLogin(data);
      console.log(res);
      if (res.code === 200) {
        commit("Userinfo", res);
      }
    },
    async getNews({ commit }, limt) {
      let res = await reqNews(limt);
      console.log(res)
      if (res.code === 200) {
        commit("News", res.results);
      }
    },
    async getVip({ dispatch, state }) {
      let { user, pass } = state.userinfo.userinfo;
      console.log(user);
      let res = await reqVip(user);
      if (res.code === 200) {
        dispatch("getUser", { user, pass });
      }
    },
  },
  modules: {},
});
